import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../presenter/app_presenter.dart';
import '../repository/rest_client.dart';
import '../repository/token_repository.dart';
import 'cache_usecase.dart';
import 'token_usecase.dart';
import 'usecase.dart';

class AppInitUseCase implements UseCase<void, NoParams> {
  final GetCacheUseCase _getCacheUseCase;
  final UpdateAjaxClientInfoUseCase _updateAjaxClientInfoUseCase;
  final GetTokenUseCase _getTokenUseCase;
  final AppPresenter _presenter;

  AppInitUseCase(this._getCacheUseCase, this._updateAjaxClientInfoUseCase,
      this._getTokenUseCase, this._presenter);
  @override
  void call(NoParams params) async {
    final ajaxInfo = await _getCacheUseCase(I18n.ajax_info);
    if (ajaxInfo.isNotEmpty) {
      await _updateAjaxClientInfoUseCase(ajaxInfo);
    }
    final token = await _getTokenUseCase(NoParams());
    _presenter.currentToken = token;
    _presenter.onComplete();
  }
}

class AppPageChangeUseCase extends UseCase<void, int> {
  final AppPresenter appPresenter;
  final TokenRepository tokenRepository;
  final RestClient client;

  AppPageChangeUseCase(this.appPresenter, this.tokenRepository, this.client);
  @override
  void call(params) async {
    appPresenter.current = params;
    final token = await GetTokenUseCase(tokenRepository, client)(NoParams());
    if (appPresenter.currentToken != token) {
      appPresenter.currentToken = token;
    }
    appPresenter.onComplete();
  }
}
